﻿//---------------------------------------------------------------------------------------------
//  Copyright 2009 Jeremy Palenchar
//  http://identitynotes.palenchar.net
//
//  This file is part of the Identity Notes Identity Notes Sample Custom Activity Library.
//
//  The Identity Notes Sample Custom Activity Library is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  The Identity Notes Sample Custom Activity Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with The Identity Notes Sample Custom Activity Library.  If not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.ResourceManagement.WebServices.WSResourceManagement;
using System.Collections.Generic;

namespace IdentityNotes.Examples.CustomActivityLibrary
{
    public partial class ResetADPasswordActivity : SequenceActivity
    {
        public ResetADPasswordActivity()
        {
            InitializeComponent();

        }

        public static DependencyProperty PasswordLengthProperty = DependencyProperty.Register("PasswordLength", typeof(System.Int32), typeof(CustomActivityLibrary.ResetADPasswordActivity));

        [FIMActivityUIAttribute(ControlType.String, 2, 1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Properties")]
        public Int32 PasswordLength
        {
            get
            {
                return ((int)(base.GetValue(CustomActivityLibrary.ResetADPasswordActivity.PasswordLengthProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.ResetADPasswordActivity.PasswordLengthProperty, value);
            }
        }

        public static DependencyProperty ADMANameProperty = DependencyProperty.Register("ADMAName", typeof(System.String), typeof(CustomActivityLibrary.ResetADPasswordActivity));

        [FIMActivityUIAttribute(ControlType.String, 24, 1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Properties")]
        public String ADMAName
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.ResetADPasswordActivity.ADMANameProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.ResetADPasswordActivity.ADMANameProperty, value);
            }
        }


        public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(System.String), typeof(CustomActivityLibrary.ResetADPasswordActivity));

        [FIMActivityUIAttribute(ControlType.String, 50, 1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public String From
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.ResetADPasswordActivity.FromProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.ResetADPasswordActivity.FromProperty, value);
            }
        }

        public static DependencyProperty MobileProviderAttributeProperty = DependencyProperty.Register("MobileProviderAttribute", typeof(System.String), typeof(CustomActivityLibrary.ResetADPasswordActivity));

        [FIMActivityUIAttribute(ControlType.String, 24, 1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Properties")]
        public String MobileProviderAttribute
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.ResetADPasswordActivity.MobileProviderAttributeProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.ResetADPasswordActivity.MobileProviderAttributeProperty, value);
            }
        }

        public static DependencyProperty SubjectProperty = DependencyProperty.Register("Subject", typeof(System.String), typeof(CustomActivityLibrary.ResetADPasswordActivity));

        [FIMActivityUIAttribute(ControlType.String, 50, 1)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public String Subject
        {
            get
            {
                return ((string)(base.GetValue(CustomActivityLibrary.ResetADPasswordActivity.SubjectProperty)));
            }
            set
            {
                base.SetValue(CustomActivityLibrary.ResetADPasswordActivity.SubjectProperty, value);
            }
        }

        public static DependencyProperty RequestProperty = DependencyProperty.Register("Request", typeof(RequestType), typeof(ResetADPasswordActivity));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Properties")]
        public RequestType Request
        {
            get
            {
                return ((RequestType)(base.GetValue(RequestProperty)));
            }
            set
            {
                base.SetValue(RequestProperty, value);
            }
        }

        //private static DependencyProperty RequestTargetProperty = DependencyProperty.Register("RequestTarget", typeof(System.Guid), typeof(CustomActivityLibrary.getRequestTargetActivity));

        //[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        //[BrowsableAttribute(true)]
        //[CategoryAttribute("Properties")]
        //private System.Guid RequestTarget
        //{
        //    get
        //    {
        //        return ((System.Guid)(base.GetValue(CustomActivityLibrary.getRequestTargetActivity.RequestTargetProperty)));
        //    }
        //    set
        //    {
        //        base.SetValue(CustomActivityLibrary.getRequestTargetActivity.RequestTargetProperty, value);
        //    }
        //}

        //private static DependencyProperty RequestCreatorProperty = DependencyProperty.Register("RequestCreator", typeof(System.Guid), typeof(CustomActivityLibrary.getRequestTargetActivity));

        //[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        //[BrowsableAttribute(true)]
        //[CategoryAttribute("Properties")]
        //private System.Guid RequestCreator
        //{
        //    get
        //    {
        //        return ((System.Guid)(base.GetValue(CustomActivityLibrary.getRequestTargetActivity.RequestCreatorProperty)));
        //    }
        //    set
        //    {
        //        base.SetValue(CustomActivityLibrary.getRequestTargetActivity.RequestCreatorProperty, value);
        //    }
        //}

        Guid getRequestCreator()
        {
            return (System.Guid)this.Request.Creator;
        }

        Guid getRequestTarget()
        {
            return (System.Guid)this.Request.Target;

        }


        private void getPropertiesFromResource_ExecuteCode(object sender, EventArgs e)
        {
            ResourceType targetResource = this.readResorurceFromRequest.Resource;
            if (targetResource["MVObjectID"] != null)
            {
                this.resetADPassword.MVGUID = targetResource["MVObjectID"] as string;
            }
            else
                throw new Exception("Cannot reset AD Password - user's MVGUID is not present");

            if (targetResource["MobilePhone"] != null)
            {
                this.SendMailToMobilePhone.CellNumber = targetResource["MobilePhone"] as string;
            }
            else
                throw new Exception("Cannot reset AD Password - user's MobilePhone attribute is not present");

            if (targetResource["MobilePhoneProvider"] != null)
            {
                this.SendMailToMobilePhone.CellOperator = targetResource[MobileProviderAttribute] as string;
            }
            else
                throw new Exception("Cannot reset AD Password - user's MobilePhoneProvider attribute is not present");

        }

        private void GenerateEmailBody_ExecuteCode(object sender, EventArgs e)
        {
            this.SendMailToMobilePhone.EmailBody = "New AD Password: " + this.GenerateADPassword.RandomPassword;
        }

        public Guid RequestTarget
        {
            get
            {
                if (this.Request == null)
                    return default(Guid);
                else return (Guid)this.Request.Target;
            }
        }

        public Guid RequestCreator
        {
            get
            {
                if (this.Request == null)
                    return default(Guid);
                else return (Guid)this.Request.Creator;
            }
        }


        public string UsersMVGUID
        {
            get
            {
                if (this.readResorurceFromRequest.Resource == null)
                    return default(String);
                else return this.readResorurceFromRequest.Resource["MVObjectID"] as string;
            }
        }

        public string UsersMobilePhoneNumber
        {
            get
            {
                if (this.readResorurceFromRequest.Resource == null)
                    return default(String);
                else return this.readResorurceFromRequest.Resource["MobilePhone"] as string;
            }
        }

        public string UsersMobilePhoneProvider
        {
            get
            {
                if (this.readResorurceFromRequest.Resource == null)
                    return default(String);
                else return this.readResorurceFromRequest.Resource[MobileProviderAttribute] as string;
            }
        }

        private void verifyRequiredAttributes_ExecuteCode(object sender, EventArgs e)
        {
            if (this.UsersMVGUID == null)
                throw new Exception("Cannot reset AD Password - user's MVGUID is not present");

            if (this.UsersMobilePhoneNumber == null)
                throw new Exception("Cannot reset AD Password - user's MobilePhone attribute is not present");

            if (this.UsersMobilePhoneProvider == null)
                throw new Exception("Cannot reset AD Password - user's MobilePhoneProvider attribute is not present");

        }
    }
}